<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'
import store, { STORE_KEY } from '@store'
import { service, common } from '@utils'

const visible = ref(false)
const formRef = ref()

const user = computed(() => store.state.business.user)

const state = reactive({
    account: {
        oldPwd: '',
        newPwd: '',
        confirmNewPwd: ''
    }
})

const validatePwd = (rule, value, callback) => {
    if (!value) {
        callback(new Error('请输入新密码'))
    } else if (value.length < 6) {
        callback(new Error('请输入至少6位数密码'))
    } else {
        callback()
    }
}

const validateRePassword = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请输入确认新密码'))
    } else if (value.length < 6) {
        callback(new Error('请输入至少6位数密码'))
    } else if (state.account.newPwd != value) {
        callback(new Error('新密码和确认密码不一致'))
    } else {
        callback()
    }
}

const rules = {
    oldPwd: [{ required: true, message: '请输入', trigger: 'blur' }],
    newPwd: [{ required: true, validator: validatePwd, trigger: 'blur' }],
    confirmNewPwd: [{ required: true, validator: validateRePassword, trigger: 'blur' }]
}

const open = () => {
    visible.value = true
}

const close = () => {
    visible.value = false
    state.account = {}
}

const onSubmit = () => {
    formRef.value.validate((valid) => {
        valid && realSubmit()
    })
}

const realSubmit = async () => {
    const { oldPwd, newPwd,confirmNewPwd } = state.account
    const res = await service.user.updatePassword(oldPwd, newPwd,confirmNewPwd)
    if (res) {
        ElMessage.success({
            message: '修改成功',
            type: 'success'
        })
        common.toLogin()
    }
}
//把方法暴露给父组件
defineExpose({
    open
})
</script>

<template>
    <el-dialog v-model="visible" title="修改密码" center :close-on-click-modal="false" @close="close" destroy-on-close width="30%" top="20vh">
        <el-form :model="state.account" :rules="rules" ref="formRef" label-width="30%" onsubmit="return false;">
            <el-form-item prop="oldPwd" label="原密码">
                <el-input type="password" v-model.trim="state.account.oldPwd" placeholder="请输入原密码" show-password>
                </el-input>
            </el-form-item>
            <el-form-item prop="newPwd" label="新密码">
                <el-input type="password" v-model.trim="state.account.newPwd" placeholder="请输入新密码" show-password>
                </el-input>
            </el-form-item>
            <el-form-item prop="confirmNewPwd" label="确认密码">
                <el-input type="password" v-model.trim="state.account.confirmNewPwd" placeholder="请输入确认密码" show-password>
                </el-input>
            </el-form-item>
        </el-form>

        <template #footer>
            <span class="flex-align-end">
                <div class="margin-right20">
                    <el-button @click="close">取消</el-button>
                </div>
                <el-button type="primary" @click="onSubmit">确定</el-button>
            </span>
        </template>
    </el-dialog>

</template>